<div id="set_optician" style="display:none;">
	<div class="widget-body  am-fr">
        <form class="am-form" data-am-validator id="formDoSubmitDialogOptician" style="padding-top:0;">
            <div class="row am-center">
                  <label class="am-radio-inline">
                    <input type="radio"  value="0" name="is_optician" required> 未配镜
                  </label>
                  <label class="am-radio-inline">
                    <input type="radio"  value="1" name="is_optician" > 待加工
                  </label>
                  <label class="am-radio-inline">
                     <input type="radio"  value="2" name="is_optician" > 待取镜
                  </label>
                  <label class="am-radio-inline">
                     <input type="radio"  value="3" name="is_optician" > 已完成
                  </label>
              </div>
        </form>
    </div>
</div>

<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var isOptician = 0;
	var nowRowData;
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : '{{ urlfor "RetailController.Optometry"}}'
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			
			var params = checkQueryTableData(aoData)
			
			//$('#search_key').val() ? params.Filter = $('#search_key').val() : ''
			
			params.IsOptician = isOptician+''; //设置状态值
			
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"mDataProp" : "Id", "bSortable": false}
			,{"data" : "DocumentNumber","bSortable": false}
			,{"bSortable": false, render:function(data, type, full, meta) {
				return full.Archives.Realname;	
			}}
			,{"bSortable": false, render:function(data, type, full, meta) {
				return full.Archives.Sex;	
			}}
			,{"bSortable": false, render:function(data, type, full, meta) {
				return full.Archives.Mobile;	
			}}
			,{"mDataProp" : "Doctor", "bSortable": false}
			,{"mDataProp" : "Optometrist", "bSortable": false}
			,{"mDataProp": "CreatedAt", render:function(data, type, full, meta) {
				return timetransDate(data);	
			}}
			,{"mDataProp": "CreatedBy", "bSortable": false}  
			,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"12%",
				"render": function ( data, type, full, meta ) { 
					btns = '<a href="/optometry/edit/?opt_id='+ full.Id +'" class="am-btn am-btn-secondary am-btn-xs">编辑</a>';
					switch(full.IsOptician){
						case 0:
							
						break;
						case 1:
							
						break;
						case 2:
							
						break;
					}
									
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	
	$('#dataTables tbody').on('click','tr', function () {
		var data = tables.row(this).data();
		$('#dataTables tbody tr').removeClass('am-warning');
		$(this).addClass('am-warning')
		
		nowRowData = data;
	});
	
	$('#dataTables tbody').on('dblclick','tr', function () {
		var data = tables.row(this).data();
		$('#dataTables tbody tr').removeClass('am-warning');
		$(this).addClass('am-warning')
		
		nowRowData = data;
		
		switch(data.IsOptician){
			case 0:  //未配镜，弹出验光数据
				viewOptometry();
			break;
			case 1:  //订单数据
				viewRetail();
			break;
			case 2: //订单数据
				viewRetail();
			break;
			case 3: //订单数据
				viewRetail();
			break;
		}
	});
	
	function viewOptometry() {
		height = "80%";
		layer.open({
        	type: 2
			, title: '验光数据'
			, shade: 0.2
			, id: 'viewOptometry_dialog'
			, area: ['90%', height]
			, content: ['{{ urlfor "OptometryController.View"}}?opt_id='+nowRowData.Id]
			, anim: 1 //动画类型
		});
		return false;	
	}
	
	function viewRetail() {
		height = "60%";
		layer.open({
        	type: 2
			, title: '消费数据'
			, shade: 0.2
			, id: 'viewRetail_dialog'
			, area: ['90%', height]
			, content: ['{{ urlfor "RetailController.View"}}?opt_id='+nowRowData.Id]
			, anim: 1 //动画类型
		});
		return false;
	}
	

	$('#doc-my-tabs').find('a').on('opened.tabs.amui', function(e) {
		var id = $(this).data('id');
		isOptician = id; //设置配镜状态
		if(isOptician == 0) {
			$('.select_glasses').removeClass('am-disabled');
		}  else {
			$('.select_glasses').addClass('am-disabled');
		}
		
		reloadTable();
	})
	
	function reloadTable() {
		$("#dataTables").dataTable().fnDraw(false);
	}
	
	function dialogReback() {
		reloadTable();
	}
	
	$('#refresh').click(function(){
		reloadTable();
		return false;
	});
	
	$('.select_glasses').click(function(){
		if(typeof(nowRowData) == 'undefined') {
			layer.msg('请选择一条验光记录！');
			return false;	
		} else {
			addSales(nowRowData.Archives.Id,nowRowData.Id);	
		}
		return false;
	});
	
	//设置状态
	function setOptician(t,IsOptician) {

		layer.open({
			type: 1
			, title: '设置状态'
			, shadeClose: false
			, shade: 0.2
			, btn: ['设置', '取消']
			, yes: function(index, layero){

				$.post('{{urlfor "RetailController.SetOptician"}}', {id:t, opt:$('input[name="is_optician"]:checked').val()}, function(j) {
					layer.msg(j.msg);
					if(j.code == 0) {
						setTimeout(function(){
							reloadTable();
							layer.close(index);
						}, 1000)	
					}
				},'json')
			}
			, id: 'set_optician_dialog'
			, area: ['410px', '160px']
			, content: $('#set_optician')
			, anim: 1 //动画类型
		});
		
		$('input[name="is_optician"]').each(function(){ if($(this).attr("value") == IsOptician) { $(this).prop("checked", true) }  });
		return false;
	}
	
	//添加销售
	function addSales(arid, optid) {
		if(nowRowData) {
			arid = nowRowData.Archives.Id;
			optid = nowRowData.Id;
		}
		location = '{{ urlfor "RetailController.AddSales"}}?arid='+ arid +'&optid='+optid;
		return false;
	}
	
	function viewOptometry_dialog(opt_id, arc_id) {
		height = "90%";
		layer.open({
			type: 2
			, title: '验光数据'
			, shade: 0.2
			, id: 'viewOptometry_dialog'
			, area: ['96%', height]
			, content: ['{{ urlfor "OptometryController.View"}}?opt_id='+opt_id+'&arc_id='+arc_id]
			, anim: 1 //动画类型
		});
		
	}
	
	//前台退货
	function cutSales() {
		location = '{{ urlfor "RetailController.Refund"}}';
		return false;
	}
	
</script>